const { request } = getApp().globalData;
var WxSearch = require('../../wxSearch/wxSearch.js');
Page({
  data: {
      boolean:true,
  },
  // 离我最近findNear
  // 按评分
  async orderByScore() {
    let res = await request('shops?_sort=score&_order=desc&categoryId=' + this.data.id)
    this.setData({
      shops: res.data
    })
  },
  async findNear() {
    // 或取当前精度纬度
    wx.getLocation({
      success: async res => {
        console.log(res)
        let res2 = await request('shops/near', {
          params: {
            categoryId: this.data.id,
            longitude: res.longitude,
            latitude: res.latitude
          }
        })
        this.setData({
          shops: res2.data,
        });
      }
    })

  },

  async doSeachByQuery(e) {
    let  q=this.data.wxSearchData.value;
    let  s=this.data.wxSearchData.view.isShow;
    console.log(q)
    let result = await request('shops', {
      params: {
        name_like:q,
        _start: 0,
        _limit: 5
      }
    });

    this.setData({
      shops: result.data,

    });
  },
  // 保存查找条件
  doSearch() {
    // console.log(this.data.inputVal)
    let v = this.data.inputVal.trim();
    // some函数 ,满足一个就为true 
    if (!(v === '' || this.data.questions.some(e => e == v))) {
      // 加入到数组中
      this.data.questions.push(this.data.inputVal);
    }

    // 保存数据
    wx.setStorageSync('q', JSON.stringify(this.data.questions));
    this.doSeachByQuery();
  },


  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    this.setData({
      inputVal: "",
      inputShowed: false
    });
  },
  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },
  inputTyping: function (e) {
    // 过滤查找列表
    let value = e.detail.value;
    // 过滤数组
    this.data.tempArr = this.data.questions.filter(q => {
      //  月亮  || ''
      return q.includes(value);
    });

    this.setData({
      inputVal: value,
      showQuestion: this.data.tempArr
    });
  },
  /**
   * 页面的初始数据
   */
  data: {
    questions: JSON.parse(wx.getStorageSync('q') || '[]'),
    _start: 0,
    _limit: 5, // 页显示数
    inputShowed: false,
    inputVal: "",
    shops: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    var that = this;
    WxSearch.init(that, 43, ['美食', '洗浴店', '结婚啦', '找工作', '租房']);
    WxSearch.initMindKeys(['洗浴店', '美食', '微信开发', '找工作']);
    let { id } = options;
    this.data.id = id;
    this.loadByPage(); //1
  },
  async loadByPage() {
    let { _start, _limit } = this.data;
    let result = await request('shops', {
      params: {
        categoryId: this.data.id,
        _start,
        _limit
      }
    });
    this.setData({
      shops: _start === 0 ? result.data : this.data.shops.concat(result.data),
      _start: _start + _limit
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.loadByPage();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  wxSearchFn: function (e) {
    var that = this
    WxSearch.wxSearchAddHisKey(that);
  },
  wxSearchInput: function (e) {
    var that = this
    WxSearch.wxSearchInput(e, that);
  },
  wxSerchFocus: function (e) {
    var that = this
    WxSearch.wxSearchFocus(e, that);
  },
  wxSearchBlur: function (e) {
    var that = this
    WxSearch.wxSearchBlur(e, that);
  },
  wxSearchKeyTap: function (e) {
    var that = this
    WxSearch.wxSearchKeyTap(e, that);
  },
  wxSearchDeleteKey: function (e) {
    var that = this
    WxSearch.wxSearchDeleteKey(e, that);
  },
  wxSearchDeleteAll: function (e) {
    var that = this;
    WxSearch.wxSearchDeleteAll(that);
  },
  wxSearchTap: function (e) {
    var that = this
    WxSearch.wxSearchHiddenPancel(that);
  }
})